package com.jlkf.service.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jlkf.dao.RoleMapper;
import com.jlkf.pojo.TblRole;
import com.jlkf.pojo.TblSysUser;
import com.jlkf.service.RoleService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.MD5Util;
import com.platform.server.util.StringTools;

@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleMapper roleMapper;
	
	/**
	 * 角色列表
	 */
	@Override
	public List<Map<String, Object>> roleList(Map<String,Object> map) {
		return roleMapper.roleList(map);
	}

	/**
	 * 添加角色
	 */
	@Override
	public int addRole(Map<String, Object> map) {
		return roleMapper.addRole(map);
	}

	/**
	 * 删除角色
	 */
	@Override
	public int deletedRole(Integer id) {
		return roleMapper.deletedRole(id);
	}

	/**
	 * 角色详情
	 */
	@Override
	public Map<String, Object> roleDetail(Integer id) {
		return roleMapper.roleDetail(id);
	}

	/**
	 * 编辑角色
	 */
	@Override
	public int updateRole(Map<String, Object> map) {
		return roleMapper.updateRole(map);
	}

	/**
	 * 用户列表
	 */
	@Override
	public List<Map<String, Object>> userList(Map<String, Object> map) {
		return roleMapper.userList(map);
	}

	/**
	 * 删除用户
	 */
	@Override
	public int deleteSysUser(Integer id) {
		return roleMapper.deleteSysUser(id);
	}

	/**
	 * 重置密码
	 */
	@Override
	public int retirePassword(String id,String password) {
		return roleMapper.retirePassword(id,password);
	}

	/**
	 * 添加系统用户
	 */
	@Override
	public int addSysUser(Map<String, Object> map) {
		return roleMapper.addSysUser(map);
	}

	/**
	 * 编辑系统用户
	 */
	@Override
	public int updateSysUser(Map<String, Object> map) {
		return roleMapper.updateSysUser(map);
	}

	/**
	 * 登录
	 */
	@Override
	public ReturnData loginUser(Map<String, Object> map, HttpServletRequest request) throws Exception {
		if(StringTools.isEmpty(map.get("phone").toString())){
			return new ReturnData(201,"电话号码不能为空!");
		}
		if(StringTools.isEmpty(map.get("password").toString())){
			return new ReturnData(203,"密码不能为空!");
		}
		String password = map.get("password").toString();
		TblSysUser userinfo = roleMapper.loginUser(map.get("phone").toString(), MD5Util.getMD5(password));
		if(userinfo != null){
			if(userinfo.getgStatus() == 2) {
				return new ReturnData(0, "该账号已被禁用");
			}
			HttpSession session = request.getSession();
			session.setMaxInactiveInterval(-1);
			session.setAttribute("loginAuth", "true");
			session.setAttribute("sysUser", userinfo);
			//获取该用户的权限
			Integer roleId = userinfo.getgRole();
			if(roleId != 0){
				TblRole roleinfo = roleMapper.selectRoleDetails(roleId);
				if(roleinfo != null) {
					String sourceId = roleinfo.getgAuthorityId();
					String[] split = sourceId.split(",");
					String  names = "";
			     	for (String string : split) {
						if(!StringTools.isEmpty(string) && !string.equals("0")) {
							Map<String,Object> sourceinfo = roleMapper.selectSystemSourse(Integer.valueOf(string));
							if(sourceinfo != null){
								String url = sourceinfo.get("url").toString();
								names +=  url + ",";
							}
						}
					}
			     	roleinfo.setgContent(names);
					session.setAttribute("r", roleinfo);
				}
			}
			return new ReturnData(userinfo,200,"登录成功");
		}else{
			return new ReturnData(204,"账号不存在或密码不正确");
		}
	}
	

}
